package com.robgfx.coalex.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

public class TranslucentButton extends Composite implements MouseListener {
	public class ImageCanvas extends Canvas {
		
		private Image highlight;

		public ImageCanvas(Composite parent, final Image img) {
			super(parent, SWT.NONE);
			if(img != null)
				highlight = getHighlightedImage(parent.getDisplay(),img.getImageData(), img.getBounds().width, img.getBounds().height);
			addPaintListener(new PaintListener() {
				public void paintControl(PaintEvent e) {
					if(isHighlighted() && highlight != null)
						e.gc.drawImage(highlight, 0, 0);
					else if(img != null)
						e.gc.drawImage(img,0,0);
				}
			});

			ImageCanvas.this.setLayoutData(new RowData(img.getBounds().width, 
					img.getBounds().height+30));
		}

		private Image getHighlightedImage(Device device, ImageData inputData, int width, int height) {
			int[] pixels = new int[width];
			for(int y=0; y<height; y++) {
				inputData.getPixels(0,y,width,pixels,0);
				for(int i=0; i<pixels.length; i++) {
					int red = highlightPixel((pixels[i] & 0x00FF0000) >> 16);
					int green = highlightPixel((pixels[i] & 0x0000FF00) >> 8);
					int blue = highlightPixel((pixels[i] & 0x000000FF) >> 0);
					inputData.setPixel(i,y,(red << 16 | green << 8 | blue));
				}
			}
			return new Image(device,inputData);
		}

		private int highlightPixel(int input) {
			float c2 = 0.025f;
			int c = (int) (255/Math.log(c2*(255+1)));
			int o = (int) (c*Math.log(c2*(input+1)));
			o = Math.min(255, o);
			o = Math.max(0, o);
			return o;
		}
	}

	private boolean isHighlighted = false;

	public TranslucentButton(Composite parent, String label, final Image img) {
		super(parent,SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		this.setLayout(layout);
		GridData data = new GridData();
		data.widthHint = img.getBounds().width;
		data.heightHint = img.getBounds().height;
		data.horizontalAlignment = GridData.CENTER;
		ImageCanvas imageComponent = new ImageCanvas(this, img);
		imageComponent.setLayoutData(data);
		imageComponent.addDisposeListener(new DisposeListener() {

			public void widgetDisposed(DisposeEvent e) {
				img.dispose();				
			}
			
		});
		Label labelComponent = new Label(this, SWT.None);
		labelComponent.setText(label);
		GridData labelData = new GridData();
		labelData.horizontalAlignment = GridData.CENTER;
		final Font font = new Font(parent.getDisplay(),"Sans", 12, SWT.BOLD | SWT.ITALIC);
		labelComponent.setFont(font);
		labelComponent.addDisposeListener(new DisposeListener() {

			public void widgetDisposed(DisposeEvent e) {
				font.dispose();
			}
			
		});
		labelComponent.setLayoutData(labelData);
		
		addMouseListener(this);
//		setSize(imageComponent.getSize());
	}
	
	public void addMouseListener(MouseListener listener) {
		for (int i = 0; i < getChildren().length; i++) {
			Control c = getChildren()[i];
			c.addMouseListener(listener);
		}
	}

	public void mouseDoubleClick(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseDown(MouseEvent e) {
		this.isHighlighted = true;
		this.redraw(0,0,getBounds().width,getBounds().height,true);
	}

	public void mouseUp(MouseEvent e) {
		this.isHighlighted = false;
		this.redraw(0,0,getBounds().width,getBounds().height,true);
	}

	public boolean isHighlighted() {
		return isHighlighted;
	}
}
